# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        '''
            递归
            递归函数定义，返回以head为节点的反转链表
        '''

        if head == None or head.next == None:
            return head
        last = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return last
    
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        '''
            迭代
        '''
        t = None
        p = head
        while p:
            q = p.next
            p.next = t
            t = p
            p = q
        
        return p
    